package com.ruoyi.com.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.com.domain.vo.*;
import com.ruoyi.com.mapper.ComMapper;
import com.ruoyi.com.service.IComService;
import com.ruoyi.common.enums.CommonBusiEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.mapper.SysDepartmentRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 公共Service业务层处理
 *
 * @author sunye
 * @date 2024-10-21
 */
@Service
public class ComServiceImpl  implements IComService
{
    @Autowired
    private ComMapper comMapper;

    @Value("${system.parentMenuId}")
    String parentMenuId;


    @Override
    public List<CustomTerminalMainInfoPO> terminalMainList(TerminalMainInfoListQuery terminalMainInfoListQuery) {
        List<CustomTerminalMainInfoPO> customTerminalMainInfoPOList = new ArrayList<>();
        Integer terminalType = terminalMainInfoListQuery.getTerminalType();
        if(terminalType ==null){
           return customTerminalMainInfoPOList;
        }
        Long deptId = terminalMainInfoListQuery.getDeptId();
        if(CommonBusiEnum.APPLY_TERMINAL_XCX.getCode() == terminalType){
            //小程序
             customTerminalMainInfoPOList =  comMapper.getXcxMainBaseList(deptId);
        }else if(CommonBusiEnum.APPLY_TERMINAL_DLDP.getCode() == terminalType){
            //导览大屏
            customTerminalMainInfoPOList =  comMapper.getDeviceInfoList(deptId);
        }
        return customTerminalMainInfoPOList;
    }


    @Override
    public String getTerminalName(Long terminalId, Long terminalType) {
        if(CommonBusiEnum.APPLY_TERMINAL_XCX.getCode() == terminalType){
            //小程序
            return  comMapper.getXcxMainName(terminalId);
        }else if(CommonBusiEnum.APPLY_TERMINAL_DLDP.getCode() == terminalType){
            //导览大屏
            return comMapper.getDeviceInfoName(terminalId);
        }
        return null;
    }

    @Override
    public List<GetAreaVO> getAreaList(GetAreaQuery query) {
        if (query.getLevel() == null || query.getLevel() < 1 || query.getLevel() > 3) {
            query.setLevel(2);
        }
        List<Integer> levels;
        if (query.getLevel() == 1) {
            levels = Collections.singletonList(1);
        } else if (query.getLevel() == 2) {
            levels = Arrays.asList(1, 2);
        } else {
            levels = Arrays.asList(1, 2, 3);
        }
        Integer[] array = levels.toArray(new Integer[0]);
        List<SysAreaDictionary> sysAreaDictionaryList =  comMapper.selectSysAreaDictionary(array);
        if (CollectionUtils.isEmpty(sysAreaDictionaryList)) {
            return Collections.emptyList();
        }
        //获取一级省份信息
        List<GetAreaVO> rootList = new ArrayList<>();
        sysAreaDictionaryList.forEach(item -> {
            if (item.getLevel() == 1) {
                GetAreaVO area = new GetAreaVO();
                area.setAreaId(item.getId());
                area.setAreaName(item.getCityName());
                area.setParentId(item.getParentId());
                area.setChild(null);
                rootList.add(area);
            }
        });
        rootList.forEach(root -> root.setChild(getAreaChildren(root, sysAreaDictionaryList)));
        return rootList;
    }

    @Override
    public List<OperateSystemFunctionVo> getFrontFunctionList(FrontFunctionQuery query) {
        //type=1 小程序 2web 3公众号 4导览大屏 5后端
        String type = "";
        if(parentMenuId.equals("1")){
            type = "1";
        }else if(parentMenuId.equals("4")){
            type = "4";
        }else{
            throw new ServiceException("该系统暂无暂无前端功能列表");
        }
        query.setType(type);
        return comMapper.getFrontFunctionList(query);
    }


    /**
     * 递归查找所有子集
     *
     * @param area
     * @param sysAreaDictionaryList
     * @return
     */
    private List<GetAreaVO> getAreaChildren(GetAreaVO area, List<SysAreaDictionary> sysAreaDictionaryList) {
        List<GetAreaVO> childrenList = new ArrayList<>();
        //查找当前节点下的子节点
        sysAreaDictionaryList.forEach(item -> {
            if (area.getAreaId().compareTo(item.getParentId()) == 0) {
                GetAreaVO child = new GetAreaVO();
                child.setAreaId(item.getId());
                child.setAreaName(item.getCityName());
                child.setParentId(area.getAreaId());
                child.setChild(null);
                childrenList.add(child);
            }
        });
        if (childrenList.size() > 0) {
            childrenList.forEach(child -> child.setChild(getAreaChildren(child, sysAreaDictionaryList)));
            return childrenList;
        } else {
            return null;
        }
    }
}
